package com.yingli.android_studying.recycler.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yingli.android_studying.R;
import com.yingli.android_studying.recycler.DynamicBean;
import com.yingli.android_studying.recycler.adapter.viewholder.SampleViewHolder;

import java.util.List;

public class SampleAdapter extends RecyclerView.Adapter<SampleViewHolder> {
    //创建每一项的视图对象，并返回ViewHolder的实例（注：ViewHolder即就是列表的每一项对象）

    private Context context;
    private List<DynamicBean> beans;

    public SampleAdapter(Context context,List<DynamicBean> beans) {
        this.context = context;
        this.beans = beans;
    }

    @NonNull
    @Override
    public SampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sample,parent,false);
        return new SampleViewHolder(view);
    }

    //给每一项ViewHolder赋值
    @Override
    public void onBindViewHolder(@NonNull SampleViewHolder holder, int position) {
        DynamicBean db = beans.get(position);
        holder.tvName.setText(db.getName());
        holder.tvDate.setText(db.getDate());
        holder.tvContent.setText(db.getContent());
    }

    //返回每一项视图的类型
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    //返回列表项的数目
    @Override
    public int getItemCount() {
        return beans.size();
    }
}
